package 多线程.java;

public class DrawMoney {
    public static void main(String[] args) {
        Object o = new Object();
        Customer t1 =new Customer(o,"A");
        Customer t2 =new Customer(o,"B");
        t1.start();
        t2.start();
    }
}
//取钱的计算方法
class  Mbank{
    private  static  int sum = 1000;
    public static   int   take(String name,int k){
        int balance = sum;
        balance = balance - k;
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        sum = balance;
        System.out.println(name+"取走了"+k+"还剩"+sum+"元");
        return sum;
    }
}

//实现多线程线程
class Customer extends  Thread{
    private String name;
    private Object a;
    public  Customer(Object a,String name){
        this.name=name;
        this.a =a;
    }
    @Override
    public void run() {
        while (true){
            synchronized (a) {
                int temp = Mbank.take(name,100);
                if (temp <= 0) {
                    break;
                }
            }
        }
         /* for (int i = 0; i <5 ; i++) {
            Mbank.take(name,100);
          }*/
    }
}
